package com.entrogy.aspect;

import com.entrogy.annotation.ThdidRequest;
import com.entrogy.bean.SchoolThdidUrlEntity;
import com.entrogy.bean.StudentBaseEntity;
import com.entrogy.bean.TeacherBasicInfoEntity;
import com.entrogy.bean.ThdidPersonEntity;
import com.entrogy.utils.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName ThdidRequest 实义德请求处理的切面
 * @Description
 * @Author luoxingjiang
 * @Date 2020-03-16 下午 6:43
 * @Version 1.0
 **/
@Aspect
@Slf4j
@Component
public class ThdidRequestAspect {

    // 配置织入点
    @Pointcut("@annotation(com.entrogy.annotation.ThdidRequest)")
    public void requestPointCut() {
    }

    @AfterThrowing(value = "requestPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
        handleException(joinPoint, e);
    }

    protected void handleException(final JoinPoint joinPoint, final Exception e) {
        try {
            // 获得注解
            ThdidRequest annotation = getAnnotation(joinPoint);
            if (annotation == null) {
                return;
            }
            // *========将实义德录入失败的人员存入数据库=========*//
            String apiUrl = annotation.value();
            Object[] args = joinPoint.getArgs();
            ThdidPersonEntity thdidPersonEntity = new ThdidPersonEntity();
            switch (apiUrl){
                case Constants.THDID_PERSON_ADD_URL:
                    log.error("实义德添加人员时出现异常=========================");
                    if (args[0] instanceof StudentBaseEntity){  // 添加学生出现异常
                        StudentBaseEntity student = (StudentBaseEntity) args[0];
                        SchoolThdidUrlEntity schoolThididUrlEntity = null;
                        if (args[1] instanceof SchoolThdidUrlEntity){
                            schoolThididUrlEntity = (SchoolThdidUrlEntity)args[1];
                        }
                        thdidPersonEntity.setBaseImage(student.getPhotoUrl()).setGender(student.getGender())
                                .setGroupId(schoolThididUrlEntity == null ? null : schoolThididUrlEntity.getStudentGroupId()).setIcNumber(student.getIcNumber())
                                .setIdNumber(student.getIdNumber()).setName(student.getStudentName()).setPhoneNumber(student.getPhoneNumber());
                    }else if (args[0] instanceof TeacherBasicInfoEntity){

                    }
                break;
            }
            // 发布事件
//            SpringContextHolder.publishEvent(new ThdidRequestExceptionEvent(thdidPersonEntity));
        } catch (Exception exp) {
            // 记录本地异常日志
            log.error("==前置通知异常==");
            log.error("异常信息:{}", exp.getMessage());
            exp.printStackTrace();
        }
    }


    /**
     * 是否存在注解，如果存在就获取
     */
    private ThdidRequest getAnnotation(JoinPoint joinPoint) throws Exception {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            return method.getAnnotation(ThdidRequest.class);
        }
        return null;
    }

}
